﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Data.Objects.SqlClient;

namespace HXDNB.DAL
{
    public class CustomerBLL : BaseDAL<Customer>
    {
        public CustomerBLL() : base("Customer") { }

        /// <summary>
        /// 查询列表
        /// </summary>
        public IQueryable<Customer> GetList(CustomerType? type)
        {
            var list = from i in _e.Customer
                       select i;
            if (type != null && (int)type > 0)
            {
                list = from i in list
                       where i.Type == (int)type
                       select i;
            }
            return list.OrderByDescending(i => i.Createtime);
        }

        /// <summary>
        /// 查询下拉列表
        /// </summary>
        public List<SelectListItem> GetForSelect(CustomerType type, SelectListItem first = null)
        {
            var list = (from i in _e.Customer
                        where i.Type == (int)type
                        orderby i.Createtime descending
                        select new SelectListItem() { Text = i.Name + (i.Phone == null ? "" : "(" + i.Phone + ")"), Value = SqlFunctions.StringConvert((decimal)i.ID).Trim() }).ToList();
            if (first != null)
            {
                list.Insert(0, first);
            }
            return list;
        }
        /// <summary>
        /// 查询下拉列表
        /// </summary>
        public List<SelectListItem> GetForSelect(List<CustomerType> type, SelectListItem first = null)
        {

            var list = from i in _e.Customer
                       select i;
            list = list.Where(i => type.Select(j => (int)j).Contains((int)(i.Type ?? 0)));
            list = list.OrderByDescending(i => i.Createtime);
            var list2 = list.ToList().Select(i => new SelectListItem() { Text = i.Name + (i.Phone == null ? "" : "(" + i.Phone + ")"), Value = i.ID.ToString() }).ToList();
            if (first != null)
            {
                list2.Insert(0, first);
            }
            return list2;
        }
    }
}
